//
//  PaintingScaleCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/4.
//

import UIKit

class PaintingScaleCell: UITableViewCell {
    
    var dataSource:[ProportionListModel] = []
    var choosePBag:( (Int) -> Void)?

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.color.main
        view.allRound(3)
        return view
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.main
        label.text = "选择比例"
        return label
    }()
    
    lazy var collectionView: UICollectionView = {
        // 初始化 CustomFlowLayout
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        // 初始化 UICollectionView 并设置 layout
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(cellWithClass: PaintingScaleItemCell.self)
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
   
        backgroundColor = .clear
        selectionStyle = .none
        contentView.addSubviews([lineView, titleLb, collectionView])
        lineView.snp.makeConstraints { make in
            make.top.equalTo(22)
            make.leading.equalTo(16)
            make.height.equalTo(16)
            make.width.equalTo(6)
        }
        
        titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(lineView)
            make.leading.equalTo(lineView.snp.trailing).offset(8)
        }
        
        let height = (SCREEN_WIDTH - 56) / 4
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(10)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(height)
        }
        // 创建一个包含所有 PaintingScale 枚举值的数组
//        let paintingScales: [PaintingScale] = [.one, .two, .three, .four]
        // 使用 map 方法来创建 PaintingScaleModel 实例并添加到 dataSource 数组中
//        self.dataSource = paintingScales.map { PaintingScaleModel(style: $0) }
        // 默认第一个选中
//        self.dataSource[0].isSelect = true
//        self.collectionView.reloadData()
    }
    
    func setup(model:[ProportionListModel]?){
        guard let models = model else { return }
        self.dataSource = models
        if let model = Appearance.shared.recordStyle {
            if let index = self.dataSource.firstIndex(where: { $0.id == model.proportion_id ?? 0 } ) {
                self.dataSource[index].isSelect = true
            }
        }
        if let _ = self.dataSource.first(where: { $0.isSelect == true }) {
            // 如果已经有选中的项，不做任何操作
        } else {
            // 如果没有选中的项，默认选中第一个
            if !self.dataSource.isEmpty {
                self.dataSource[0].isSelect = true
            }
        }
        self.collectionView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension PaintingScaleCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = dataSource[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withClass: PaintingScaleItemCell.self, for: indexPath)
        if let image = UIImage(named: "icon_painting_\(model.proportion ?? "")") {
            cell.coverIV.image = image
        }else {
            cell.coverIV.sd_setImage(with: URL(string: model.img_url ?? ""))
        }
        cell.titleLb.text = model.proportion ?? ""
        cell.contentView.layer.borderWidth = 2
        if model.isSelect {
            cell.contentView.layer.borderColor = UIColor(hexString: "#45DFFF").cgColor
        }else {
            cell.contentView.layer.borderColor = UIColor.clear.cgColor
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (SCREEN_WIDTH - 56) / 4
        return CGSize(width: width, height: width)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        for (index, value) in dataSource.enumerated() {
            dataSource[index].isSelect = false
        }
        
        dataSource[indexPath.item].isSelect = true
        self.collectionView.reloadData()
        let model = dataSource[indexPath.item]
        self.choosePBag?(model.id ?? 0)
    }
}

class PaintingScaleModel {
    var style: PaintingScale
    var isSelect:Bool
    
    init(style: PaintingScale, isSelect: Bool = false) {
           self.style = style
           self.isSelect = isSelect
       }
}

enum PaintingScale {
    case one
    case two
    case three
    case four
    
    var image: UIImage? {
        switch self {
        case .one:
            return UIImage(named: "icon_painting_1-1")
        case .two:
            return UIImage(named: "icon_painting_3-4")
        case .three:
            return UIImage(named: "icon_painting_4-3")
        case .four:
            return UIImage(named: "icon_painting_16-9")
        }
    }
 
    var title: String? {
        switch self {
        case .one:
            return "1:1"
        case .two:
            return "3:4"
        case .three:
            return "4:3"
        case .four:
            return "16:9"
        }
    }
}
